{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "按照下面的链接配置，将这个ipynb放到github下下来的ChatTTS目录下面\n",
    "\n",
    "https://blog.csdn.net/R_Here_Waiting/article/details/141096326\n",
    "\n",
    "https://blog.csdn.net/HSJ0170/article/details/142790615"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Requirement already satisfied: sox in /root/miniconda3/envs/myconda/lib/python3.10/site-packages (1.5.0)\n",
      "Requirement already satisfied: numpy>=1.9.0 in /root/miniconda3/envs/myconda/lib/python3.10/site-packages (from sox) (1.25.2)\n",
      "Requirement already satisfied: typing-extensions>=3.7.4.2 in /root/miniconda3/envs/myconda/lib/python3.10/site-packages (from sox) (4.12.2)\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n",
      "Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple\n",
      "Collecting pysoundfile\n",
      "  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/2a/b3/0b871e5fd31b9a8e54b4ee359384e705a1ca1e2870706d2f081dc7cc1693/PySoundFile-0.9.0.post1-py2.py3-none-any.whl (24 kB)\n",
      "Requirement already satisfied: cffi>=0.6 in /root/miniconda3/envs/myconda/lib/python3.10/site-packages (from pysoundfile) (1.17.1)\n",
      "Requirement already satisfied: pycparser in /root/miniconda3/envs/myconda/lib/python3.10/site-packages (from cffi>=0.6->pysoundfile) (2.22)\n",
      "Installing collected packages: pysoundfile\n",
      "Successfully installed pysoundfile-0.9.0.post1\n",
      "\u001b[33mWARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv\u001b[0m\u001b[33m\n",
      "\u001b[0mNote: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "%pip install sox\n",
    "%pip install pysoundfile"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/myconda/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "text:   0%|          | 1/384(max) [00:00,  3.79it/s]We detected that you are passing `past_key_values` as a tuple of tuples. This is deprecated and will be removed in v4.47. Please convert your cache or use an appropriate `Cache` class (https://huggingface.co/docs/transformers/kv_cache#legacy-cache-format)\n",
      "text:   2%|▏         | 7/384(max) [00:00, 19.24it/s]\n",
      "code:   2%|▏         | 37/2048(max) [00:00, 69.99it/s]\n"
     ]
    }
   ],
   "source": [
    "# 最简单的实现\n",
    "\n",
    "import torch\n",
    "import torchaudio\n",
    "import ChatTTS\n",
    "model_dir = '/mnt/【科研】机器人/ChatTTS/model'\n",
    " \n",
    "chat = ChatTTS.Chat()\n",
    "chat.load(source='local', custom_path=model_dir,compile=False)\n",
    "texts = '你好，我们是汽车销售中心的，请问您最近有购车计划吗？'\n",
    "wavs = chat.infer(texts)\n",
    "torchaudio.save(\"./OUTPUT/test.wav\", torch.from_numpy(wavs), 24000,format=\"wav\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Refine text : 控制是否对文本进行口语化处理，取消勾选则后面三个选项无效\n",
    "- oral : 控制文本口语化程度，范围为 0-9，数字越大，添加的“就是”、“那么”之类的连接词越多\n",
    "- laugh : 控制文本是否添加笑声，范围为 0-9，数字越大，笑声越多\n",
    "- break : 控制文本是否添加停顿，范围为 0-9，数字越大，停顿越多"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/root/miniconda3/envs/myconda/lib/python3.10/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
      "  from .autonotebook import tqdm as notebook_tqdm\n",
      "2024-11-05 12:01:47,133 WETEXT INFO found existing fst: /root/miniconda3/envs/myconda/lib/python3.10/site-packages/tn/zh_tn_tagger.fst\n",
      "2024-11-05 12:01:47,134 WETEXT INFO                     /root/miniconda3/envs/myconda/lib/python3.10/site-packages/tn/zh_tn_verbalizer.fst\n",
      "2024-11-05 12:01:47,134 WETEXT INFO skip building fst for zh_normalizer ...\n"
     ]
    }
   ],
   "source": [
    "# 指定音色等\n",
    "\n",
    "import torch\n",
    "import torchaudio\n",
    "import ChatTTS\n",
    "from tools.normalizer import normalizer_zh_tn\n",
    "\n",
    "model_dir = '/mnt/【科研】机器人/ChatTTS/model'\n",
    "chat = ChatTTS.Chat()\n",
    "chat.load(source='local', custom_path=model_dir,compile=False,device='cuda')\n",
    "\n",
    "# 音色：女 铿锵有力\n",
    "speaker_vector = '-4.741,0.419,-3.355,3.652,-1.682,-1.254,9.719,1.436,0.871,12.334,-0.175,-2.653,-3.132,0.525,1.573,-0.351,0.030,-3.154,0.935,-0.111,-6.306,-1.840,-0.818,9.773,-1.842,-3.433,-6.200,-4.311,1.162,1.023,11.552,2.769,-2.408,-1.494,-1.143,12.412,0.832,-1.203,5.425,-1.481,0.737,-1.487,6.381,5.821,0.599,6.186,5.379,-2.141,0.697,5.005,-4.944,0.840,-4.974,0.531,-0.679,2.237,4.360,0.438,2.029,1.647,-2.247,-1.716,6.338,1.922,0.731,-2.077,0.707,4.959,-1.969,5.641,2.392,-0.953,0.574,1.061,-9.335,0.658,-0.466,4.813,1.383,-0.907,5.417,-7.383,-3.272,-1.727,2.056,1.996,2.313,-0.492,3.373,0.844,-8.175,-0.558,0.735,-0.921,8.387,-7.800,0.775,1.629,-6.029,0.709,-2.767,-0.534,2.035,2.396,2.278,2.584,3.040,-6.845,7.649,-2.812,-1.958,8.794,2.551,3.977,0.076,-2.073,-4.160,0.806,3.798,-1.968,-4.690,5.702,-4.376,-2.396,1.368,-0.707,4.930,6.926,1.655,4.423,-1.482,-3.670,2.988,-3.296,0.767,3.306,1.623,-3.604,-2.182,-1.480,-2.661,-1.515,-2.546,3.455,-3.500,-3.163,-1.376,-12.772,1.931,4.422,6.434,-0.386,-0.704,-2.720,2.177,-0.666,12.417,4.228,0.823,-1.740,1.285,-2.173,-4.285,-6.220,2.479,3.135,-2.790,1.395,0.946,-0.052,9.148,-2.802,-5.604,-1.884,1.796,-0.391,-1.499,0.661,-2.691,0.680,0.848,3.765,0.092,7.978,3.023,2.450,-15.073,5.077,3.269,2.715,-0.862,2.187,13.048,-7.028,-1.602,-6.784,-3.143,-1.703,1.001,-2.883,0.818,-4.012,4.455,-1.545,-14.483,-1.008,-3.995,2.366,3.961,1.254,-0.458,-1.175,2.027,1.830,2.682,0.131,-1.839,-28.123,-1.482,16.475,2.328,-13.377,-0.980,9.557,0.870,-3.266,-3.214,3.577,2.059,1.676,-0.621,-6.370,-2.842,0.054,-0.059,-3.179,3.182,3.411,4.419,-1.688,-0.663,-5.189,-5.542,-1.146,2.676,2.224,-5.519,6.069,24.349,2.509,4.799,0.024,-2.849,-1.192,-16.989,1.845,6.337,-1.936,-0.585,1.691,-3.564,0.931,0.223,4.314,-2.609,0.544,-1.931,3.604,1.248,-0.852,2.991,-1.499,-3.836,1.774,-0.744,0.824,7.597,-1.538,-0.009,0.494,-2.253,-1.293,-0.475,-3.816,8.165,0.285,-3.348,3.599,-4.959,-1.498,-1.492,-0.867,0.421,-2.191,-1.627,6.027,3.667,-21.459,2.594,-2.997,5.076,0.197,-3.305,3.998,1.642,-6.221,3.177,-3.344,5.457,0.671,-2.765,-0.447,1.080,2.504,1.809,1.144,2.752,0.081,-3.700,0.215,-2.199,3.647,1.977,1.326,3.086,34.789,-1.017,-14.257,-3.121,-0.568,-0.316,11.455,0.625,-6.517,-0.244,-8.490,9.220,0.068,-2.253,-1.485,3.372,2.002,-3.357,3.394,1.879,16.467,-2.271,1.377,-0.611,-5.875,1.004,12.487,2.204,0.115,-4.908,-6.992,-1.821,0.211,0.540,1.239,-2.488,-0.411,2.132,2.130,0.984,-10.669,-7.456,0.624,-0.357,7.948,2.150,-2.052,3.772,-4.367,-11.910,-2.094,3.987,-1.565,0.618,1.152,1.308,-0.807,1.212,-4.476,0.024,-6.449,-0.236,5.085,1.265,-0.586,-2.313,3.642,-0.766,3.626,6.524,-1.686,-2.524,-0.985,-6.501,-2.558,0.487,-0.662,-1.734,0.275,-9.230,-3.785,3.031,1.264,15.340,2.094,1.997,0.408,9.130,0.578,-2.239,-1.493,11.034,2.201,6.757,3.432,-4.133,-3.668,2.099,-6.798,-0.102,2.348,6.910,17.910,-0.779,4.389,1.432,-0.649,5.115,-1.064,3.580,4.129,-4.289,-2.387,-0.327,-1.975,-0.892,5.327,-3.908,3.639,-8.247,-1.876,-10.866,2.139,-3.932,-0.031,-1.444,0.567,-5.543,-2.906,1.399,-0.107,-3.044,-4.660,-1.235,-1.011,9.577,2.294,6.615,-1.279,-2.159,-3.050,-6.493,-7.282,-8.546,5.393,2.050,10.068,3.494,8.810,2.820,3.063,0.603,1.965,2.896,-3.049,7.106,-0.224,-1.016,2.531,-0.902,1.436,-1.843,1.129,6.746,-2.184,0.801,-0.965,-7.555,-18.409,6.176,-3.706,2.261,4.158,-0.928,2.164,-3.248,-4.892,-0.008,-0.521,7.931,-10.693,4.320,-0.841,4.446,-1.591,-0.702,4.075,3.323,-3.406,-1.198,-5.518,-0.036,-2.247,-2.638,2.160,-9.644,-3.858,2.402,-2.640,1.683,-0.961,-3.076,0.226,5.106,0.712,0.669,2.539,-4.340,-0.892,0.732,0.775,-2.757,4.365,-2.368,5.368,0.342,-0.655,0.240,0.775,3.686,-4.008,16.296,4.973,1.851,4.747,0.652,-2.117,6.470,2.189,-8.467,3.236,3.745,-1.332,3.583,-2.504,5.596,-2.440,0.995,-2.267,-3.322,3.490,1.156,1.716,0.669,-3.640,-1.709,5.055,6.265,-3.963,2.863,14.129,5.180,-3.590,0.393,0.234,-3.978,6.946,-0.521,1.925,-1.497,-0.283,0.895,-3.969,5.338,-1.808,-3.578,2.699,2.728,-0.895,-2.175,-2.717,2.574,4.571,1.131,2.187,3.620,-0.388,-3.685,0.979,2.731,-2.164,1.628,-1.006,-7.766,-11.033,-10.985,-2.413,-1.967,0.790,0.826,-1.623,-1.783,3.021,1.598,-0.931,-0.605,-1.684,1.408,-2.771,-2.354,5.564,-2.296,-4.774,-2.830,-5.149,2.731,-3.314,-1.002,3.522,3.235,-1.598,1.923,-2.755,-3.900,-3.519,-1.673,-2.049,-10.404,6.773,1.071,0.247,1.120,-0.794,2.187,-0.189,-5.591,4.361,1.772,1.067,1.895,-5.649,0.946,-2.834,-0.082,3.295,-7.659,-0.128,2.077,-1.638,0.301,-0.974,4.331,11.711,4.199,1.545,-3.236,-4.404,-1.333,0.623,1.414,-0.240,-0.816,-0.808,-1.382,0.632,-5.238,0.120,10.634,-2.026,1.702,-0.469,1.252,1.173,3.015,-8.798,1.633,-5.323,2.149,-6.481,11.635,3.072,5.642,5.252,4.702,-3.523,-0.594,4.150,1.392,0.554,-4.377,3.646,-0.884,1.468,0.779,2.372,-0.101,-5.702,0.539,-0.440,5.149,-0.011,-1.899,-1.349,-0.355,0.076,-0.100,-0.004,5.346,6.276,0.966,-3.138,-2.633,-3.124,3.606,-3.793,-3.332,2.359,-0.739,-3.301,-2.775,-0.491,3.283,-1.394,-1.883,1.203,1.097,2.233,2.170,-2.980,-15.800,-6.791,-0.175,-4.600,-3.840,-4.179,6.568,5.935,-0.431,4.623,4.601,-1.726,0.410,2.591,4.016,8.169,1.763,-3.058,-1.340,6.276,4.682,-0.089,1.301,-4.817'\n",
    "speaker = torch.tensor([float(x) for x in speaker_vector.split(',')]).to('cuda')\n",
    "params_infer_code = ChatTTS.Chat.InferCodeParams(\n",
    "    prompt=\"[speed_4]\",  # 速度(0-9)\n",
    "    spk_emb=speaker,  # add sampled speaker\n",
    "    temperature=.3,  # using custom temperature\n",
    "    top_P=0.7,  # top P decode\n",
    "    top_K=20,  # top K decode\n",
    ")\n",
    "chat.normalizer.register('zh', normalizer_zh_tn())\n",
    "params_refine_text = ChatTTS.Chat.RefineTextParams(\n",
    "    prompt='[oral_3][laugh_0][break_6]',\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import time\n",
    "import torch\n",
    "import torchaudio\n",
    "\n",
    "# 可能的客服回复文本\n",
    "texts_list = [\n",
    "    '你好，我们是汽车销售中心的，请问您最近有购车计划吗？',\n",
    "    '您好，我们提供多种汽车品牌和车型，您有没有具体想要了解的车型呢？',\n",
    "    '请问您打算选择哪种付款方式呢？是全款还是分期付款？',\n",
    "    '请问您所在的城市是哪儿？这样我们可以为您提供更详细的购车信息。',\n",
    "    '请问您预计什么时候购车呢？这样我们可以根据您的时间安排试驾等服务。',\n",
    "    '如果您有任何购车上的疑问，随时可以联系我，我会为您提供详细的解答。',\n",
    "    '我们有很多优惠活动，不知道您是否感兴趣？可以为您提供更多信息。',\n",
    "    '您对车辆的配置或者其他特性有任何偏好吗？我们可以帮您找到最合适的车型。',\n",
    "    '感谢您的咨询，稍后我们将通过电话或邮件与您进一步沟通。'\n",
    "]\n",
    "\n",
    "# 用于生成语音的函数\n",
    "def generate_audio(text):\n",
    "    start_time = time.time()\n",
    "    \n",
    "    # 假设 chat.infer 是我们用于文字转语音的接口\n",
    "    wavs = chat.infer(\n",
    "        text,\n",
    "        skip_refine_text=False,\n",
    "        params_refine_text=params_refine_text,\n",
    "        params_infer_code=params_infer_code\n",
    "    )\n",
    "    \n",
    "    # 保存为wav格式\n",
    "    torchaudio.save(f\"./OUTPUT/{text[:10]}_output.wav\", torch.from_numpy(wavs), 24000, format=\"wav\")\n",
    "    \n",
    "    end_time = time.time()\n",
    "    return end_time - start_time\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "found invalid characters: {'？'}\n",
      "text:   0%|          | 1/384(max) [00:00,  1.63it/s]We detected that you are passing `past_key_values` as a tuple of tuples. This is deprecated and will be removed in v4.47. Please convert your cache or use an appropriate `Cache` class (https://huggingface.co/docs/transformers/kv_cache#legacy-cache-format)\n",
      "text:   7%|▋         | 28/384(max) [00:01, 20.90it/s]\n",
      "code:  12%|█▏        | 239/2048(max) [00:05, 44.09it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'你好，我们是汽车销售...'用时: 14.65秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:   9%|▉         | 35/384(max) [00:01, 34.55it/s]\n",
      "code:  15%|█▌        | 313/2048(max) [00:07, 43.27it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'您好，我们提供多种汽...'用时: 8.41秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:   7%|▋         | 26/384(max) [00:00, 41.95it/s]\n",
      "code:  12%|█▏        | 239/2048(max) [00:05, 44.98it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'请问您打算选择哪种付...'用时: 6.58秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:   9%|▉         | 35/384(max) [00:01, 33.16it/s]\n",
      "code:  16%|█▌        | 322/2048(max) [00:06, 46.48it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'请问您所在的城市是哪...'用时: 8.22秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:   9%|▉         | 36/384(max) [00:00, 41.75it/s]\n",
      "code:  16%|█▌        | 332/2048(max) [00:08, 41.03it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'请问您预计什么时候购...'用时: 9.13秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  10%|▉         | 38/384(max) [00:00, 47.48it/s]\n",
      "code:  16%|█▌        | 326/2048(max) [00:07, 44.01it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'如果您有任何购车上的...'用时: 8.41秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:   9%|▉         | 35/384(max) [00:00, 44.60it/s]\n",
      "code:  15%|█▍        | 298/2048(max) [00:06, 46.41it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'我们有很多优惠活动，...'用时: 7.54秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  10%|▉         | 38/384(max) [00:00, 40.40it/s]\n",
      "code:  17%|█▋        | 345/2048(max) [00:08, 42.97it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'您对车辆的配置或者其...'用时: 9.43秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:   8%|▊         | 30/384(max) [00:00, 48.78it/s]\n",
      "code:  13%|█▎        | 261/2048(max) [00:05, 43.50it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'感谢您的咨询，稍后我...'用时: 6.80秒\n"
     ]
    }
   ],
   "source": [
    "# 遍历所有文本并生成语音，记录每条语音生成的用时\n",
    "for text in texts_list:\n",
    "    elapsed_time = generate_audio(text)\n",
    "    print(f\"生成语音'{text[:10]}...'用时: {elapsed_time:.2f}秒\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2024-11-05 12:04:33,711 WETEXT INFO found existing fst: /root/miniconda3/envs/myconda/lib/python3.10/site-packages/tn/zh_tn_tagger.fst\n",
      "2024-11-05 12:04:33,711 WETEXT INFO found existing fst: /root/miniconda3/envs/myconda/lib/python3.10/site-packages/tn/zh_tn_tagger.fst\n",
      "2024-11-05 12:04:33,712 WETEXT INFO                     /root/miniconda3/envs/myconda/lib/python3.10/site-packages/tn/zh_tn_verbalizer.fst\n",
      "2024-11-05 12:04:33,712 WETEXT INFO                     /root/miniconda3/envs/myconda/lib/python3.10/site-packages/tn/zh_tn_verbalizer.fst\n",
      "2024-11-05 12:04:33,713 WETEXT INFO skip building fst for zh_normalizer ...\n",
      "2024-11-05 12:04:33,713 WETEXT INFO skip building fst for zh_normalizer ...\n",
      "found invalid characters: {'？'}\n",
      "text:  15%|█▌        | 59/384(max) [00:01, 45.60it/s]\n",
      "code:  25%|██▌       | 512/2048(max) [00:12, 40.45it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'0...'用时: 14.33秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  16%|█▌        | 60/384(max) [00:01, 37.61it/s]\n",
      "code:  27%|██▋       | 545/2048(max) [00:13, 41.58it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'1...'用时: 14.94秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  16%|█▌        | 60/384(max) [00:01, 39.90it/s]\n",
      "code:  27%|██▋       | 559/2048(max) [00:13, 41.81it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'2...'用时: 15.16秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  16%|█▌        | 60/384(max) [00:01, 46.59it/s]\n",
      "code:  25%|██▌       | 520/2048(max) [00:10, 48.74it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'3...'用时: 12.23秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  16%|█▌        | 60/384(max) [00:00, 66.27it/s]\n",
      "code:  25%|██▍       | 503/2048(max) [00:07, 63.16it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'4...'用时: 9.07秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  15%|█▌        | 58/384(max) [00:00, 61.63it/s]\n",
      "code:  25%|██▍       | 506/2048(max) [00:08, 61.32it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'5...'用时: 9.38秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  15%|█▌        | 59/384(max) [00:00, 64.57it/s]\n",
      "code:  26%|██▌       | 523/2048(max) [00:08, 59.44it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'6...'用时: 10.20秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  15%|█▌        | 58/384(max) [00:00, 66.42it/s]\n",
      "code:  25%|██▌       | 512/2048(max) [00:07, 64.25it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'7...'用时: 9.02秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  16%|█▌        | 61/384(max) [00:00, 64.22it/s]\n",
      "code:  25%|██▌       | 516/2048(max) [00:08, 57.78it/s]\n",
      "found invalid characters: {'？'}\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'8...'用时: 10.44秒\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "text:  16%|█▌        | 60/384(max) [00:01, 58.93it/s]\n",
      "code:  24%|██▍       | 500/2048(max) [00:07, 63.35it/s]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "生成语音'9...'用时: 9.05秒\n"
     ]
    }
   ],
   "source": [
    "# 测试不同的laugh\n",
    "\n",
    "# 指定音色等\n",
    "\n",
    "import torch\n",
    "import torchaudio\n",
    "import ChatTTS\n",
    "from tools.normalizer import normalizer_zh_tn\n",
    "\n",
    "model_dir = '/mnt/【科研】机器人/ChatTTS/model'\n",
    "chat = ChatTTS.Chat()\n",
    "chat.load(source='local', custom_path=model_dir,compile=False)\n",
    "\n",
    "# 音色：女 铿锵有力\n",
    "speaker_vector = '-4.741,0.419,-3.355,3.652,-1.682,-1.254,9.719,1.436,0.871,12.334,-0.175,-2.653,-3.132,0.525,1.573,-0.351,0.030,-3.154,0.935,-0.111,-6.306,-1.840,-0.818,9.773,-1.842,-3.433,-6.200,-4.311,1.162,1.023,11.552,2.769,-2.408,-1.494,-1.143,12.412,0.832,-1.203,5.425,-1.481,0.737,-1.487,6.381,5.821,0.599,6.186,5.379,-2.141,0.697,5.005,-4.944,0.840,-4.974,0.531,-0.679,2.237,4.360,0.438,2.029,1.647,-2.247,-1.716,6.338,1.922,0.731,-2.077,0.707,4.959,-1.969,5.641,2.392,-0.953,0.574,1.061,-9.335,0.658,-0.466,4.813,1.383,-0.907,5.417,-7.383,-3.272,-1.727,2.056,1.996,2.313,-0.492,3.373,0.844,-8.175,-0.558,0.735,-0.921,8.387,-7.800,0.775,1.629,-6.029,0.709,-2.767,-0.534,2.035,2.396,2.278,2.584,3.040,-6.845,7.649,-2.812,-1.958,8.794,2.551,3.977,0.076,-2.073,-4.160,0.806,3.798,-1.968,-4.690,5.702,-4.376,-2.396,1.368,-0.707,4.930,6.926,1.655,4.423,-1.482,-3.670,2.988,-3.296,0.767,3.306,1.623,-3.604,-2.182,-1.480,-2.661,-1.515,-2.546,3.455,-3.500,-3.163,-1.376,-12.772,1.931,4.422,6.434,-0.386,-0.704,-2.720,2.177,-0.666,12.417,4.228,0.823,-1.740,1.285,-2.173,-4.285,-6.220,2.479,3.135,-2.790,1.395,0.946,-0.052,9.148,-2.802,-5.604,-1.884,1.796,-0.391,-1.499,0.661,-2.691,0.680,0.848,3.765,0.092,7.978,3.023,2.450,-15.073,5.077,3.269,2.715,-0.862,2.187,13.048,-7.028,-1.602,-6.784,-3.143,-1.703,1.001,-2.883,0.818,-4.012,4.455,-1.545,-14.483,-1.008,-3.995,2.366,3.961,1.254,-0.458,-1.175,2.027,1.830,2.682,0.131,-1.839,-28.123,-1.482,16.475,2.328,-13.377,-0.980,9.557,0.870,-3.266,-3.214,3.577,2.059,1.676,-0.621,-6.370,-2.842,0.054,-0.059,-3.179,3.182,3.411,4.419,-1.688,-0.663,-5.189,-5.542,-1.146,2.676,2.224,-5.519,6.069,24.349,2.509,4.799,0.024,-2.849,-1.192,-16.989,1.845,6.337,-1.936,-0.585,1.691,-3.564,0.931,0.223,4.314,-2.609,0.544,-1.931,3.604,1.248,-0.852,2.991,-1.499,-3.836,1.774,-0.744,0.824,7.597,-1.538,-0.009,0.494,-2.253,-1.293,-0.475,-3.816,8.165,0.285,-3.348,3.599,-4.959,-1.498,-1.492,-0.867,0.421,-2.191,-1.627,6.027,3.667,-21.459,2.594,-2.997,5.076,0.197,-3.305,3.998,1.642,-6.221,3.177,-3.344,5.457,0.671,-2.765,-0.447,1.080,2.504,1.809,1.144,2.752,0.081,-3.700,0.215,-2.199,3.647,1.977,1.326,3.086,34.789,-1.017,-14.257,-3.121,-0.568,-0.316,11.455,0.625,-6.517,-0.244,-8.490,9.220,0.068,-2.253,-1.485,3.372,2.002,-3.357,3.394,1.879,16.467,-2.271,1.377,-0.611,-5.875,1.004,12.487,2.204,0.115,-4.908,-6.992,-1.821,0.211,0.540,1.239,-2.488,-0.411,2.132,2.130,0.984,-10.669,-7.456,0.624,-0.357,7.948,2.150,-2.052,3.772,-4.367,-11.910,-2.094,3.987,-1.565,0.618,1.152,1.308,-0.807,1.212,-4.476,0.024,-6.449,-0.236,5.085,1.265,-0.586,-2.313,3.642,-0.766,3.626,6.524,-1.686,-2.524,-0.985,-6.501,-2.558,0.487,-0.662,-1.734,0.275,-9.230,-3.785,3.031,1.264,15.340,2.094,1.997,0.408,9.130,0.578,-2.239,-1.493,11.034,2.201,6.757,3.432,-4.133,-3.668,2.099,-6.798,-0.102,2.348,6.910,17.910,-0.779,4.389,1.432,-0.649,5.115,-1.064,3.580,4.129,-4.289,-2.387,-0.327,-1.975,-0.892,5.327,-3.908,3.639,-8.247,-1.876,-10.866,2.139,-3.932,-0.031,-1.444,0.567,-5.543,-2.906,1.399,-0.107,-3.044,-4.660,-1.235,-1.011,9.577,2.294,6.615,-1.279,-2.159,-3.050,-6.493,-7.282,-8.546,5.393,2.050,10.068,3.494,8.810,2.820,3.063,0.603,1.965,2.896,-3.049,7.106,-0.224,-1.016,2.531,-0.902,1.436,-1.843,1.129,6.746,-2.184,0.801,-0.965,-7.555,-18.409,6.176,-3.706,2.261,4.158,-0.928,2.164,-3.248,-4.892,-0.008,-0.521,7.931,-10.693,4.320,-0.841,4.446,-1.591,-0.702,4.075,3.323,-3.406,-1.198,-5.518,-0.036,-2.247,-2.638,2.160,-9.644,-3.858,2.402,-2.640,1.683,-0.961,-3.076,0.226,5.106,0.712,0.669,2.539,-4.340,-0.892,0.732,0.775,-2.757,4.365,-2.368,5.368,0.342,-0.655,0.240,0.775,3.686,-4.008,16.296,4.973,1.851,4.747,0.652,-2.117,6.470,2.189,-8.467,3.236,3.745,-1.332,3.583,-2.504,5.596,-2.440,0.995,-2.267,-3.322,3.490,1.156,1.716,0.669,-3.640,-1.709,5.055,6.265,-3.963,2.863,14.129,5.180,-3.590,0.393,0.234,-3.978,6.946,-0.521,1.925,-1.497,-0.283,0.895,-3.969,5.338,-1.808,-3.578,2.699,2.728,-0.895,-2.175,-2.717,2.574,4.571,1.131,2.187,3.620,-0.388,-3.685,0.979,2.731,-2.164,1.628,-1.006,-7.766,-11.033,-10.985,-2.413,-1.967,0.790,0.826,-1.623,-1.783,3.021,1.598,-0.931,-0.605,-1.684,1.408,-2.771,-2.354,5.564,-2.296,-4.774,-2.830,-5.149,2.731,-3.314,-1.002,3.522,3.235,-1.598,1.923,-2.755,-3.900,-3.519,-1.673,-2.049,-10.404,6.773,1.071,0.247,1.120,-0.794,2.187,-0.189,-5.591,4.361,1.772,1.067,1.895,-5.649,0.946,-2.834,-0.082,3.295,-7.659,-0.128,2.077,-1.638,0.301,-0.974,4.331,11.711,4.199,1.545,-3.236,-4.404,-1.333,0.623,1.414,-0.240,-0.816,-0.808,-1.382,0.632,-5.238,0.120,10.634,-2.026,1.702,-0.469,1.252,1.173,3.015,-8.798,1.633,-5.323,2.149,-6.481,11.635,3.072,5.642,5.252,4.702,-3.523,-0.594,4.150,1.392,0.554,-4.377,3.646,-0.884,1.468,0.779,2.372,-0.101,-5.702,0.539,-0.440,5.149,-0.011,-1.899,-1.349,-0.355,0.076,-0.100,-0.004,5.346,6.276,0.966,-3.138,-2.633,-3.124,3.606,-3.793,-3.332,2.359,-0.739,-3.301,-2.775,-0.491,3.283,-1.394,-1.883,1.203,1.097,2.233,2.170,-2.980,-15.800,-6.791,-0.175,-4.600,-3.840,-4.179,6.568,5.935,-0.431,4.623,4.601,-1.726,0.410,2.591,4.016,8.169,1.763,-3.058,-1.340,6.276,4.682,-0.089,1.301,-4.817'\n",
    "speaker = torch.tensor([float(x) for x in speaker_vector.split(',')])\n",
    "params_infer_code = ChatTTS.Chat.InferCodeParams(\n",
    "    prompt=\"[speed_4]\",  # 速度(0-9)\n",
    "    spk_emb=speaker,  # add sampled speaker\n",
    "    temperature=.3,  # using custom temperature\n",
    "    top_P=0.7,  # top P decode\n",
    "    top_K=20,  # top K decode\n",
    ")\n",
    "chat.normalizer.register('zh', normalizer_zh_tn())\n",
    "\n",
    "\n",
    "# 用于生成语音的函数\n",
    "def laugh_generate_audio(text):\n",
    "    start_time = time.time()\n",
    "    \n",
    "    # 假设 chat.infer 是我们用于文字转语音的接口\n",
    "    wavs = chat.infer(\n",
    "        text,\n",
    "        skip_refine_text=False,\n",
    "        params_refine_text=params_refine_text,\n",
    "        params_infer_code=params_infer_code\n",
    "    )\n",
    "    \n",
    "    # 保存为wav格式\n",
    "    torchaudio.save(f\"./OUTPUT/laugh_{i}_output.wav\", torch.from_numpy(wavs), 24000, format=\"wav\")\n",
    "    \n",
    "    end_time = time.time()\n",
    "    return end_time - start_time\n",
    "\n",
    "for i in range(10):\n",
    "    params_refine_text = ChatTTS.Chat.RefineTextParams(\n",
    "        prompt='[oral_2][laugh_'+str(i)+'][break_6]',\n",
    "    )\n",
    "    text = '你好，我们是汽车销售中心的，请问您最近有购车计划吗？我们提供多种汽车品牌和车型，您有没有具体想要了解的车型呢？'\n",
    "    elapsed_time = laugh_generate_audio(text)\n",
    "    print(f\"生成语音'{i}...'用时: {elapsed_time:.2f}秒\")\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myconda",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
